package thread;

/**
 * sleep方法要求必须处理中断异常InterruptedException
 * 当一个线程调用sleep方法处于睡眠阻塞的过程中若该线程的interrupt方法被调用，
 * 那么此时会立即中断该线程的睡眠阻塞，此时sleep方法会通过抛出异常告知这一现象
 */
public class SleepDemo2 {
    public static void main(String[] args) {
        Thread yu = new Thread("余豪杰"){
            public void run(){
                System.out.println(getName()+":刚美容完睡一会吧...");
                try {
                    Thread.sleep(500000);
                } catch (InterruptedException e) {
                    System.out.println(getName()+"干嘛呢！干嘛呢！干嘛呢！都破了相了！");
                }
                System.out.println(getName()+"：睡醒了");
            }
        };
        Thread gong = new Thread("龚志颖"){
            public void run(){
                System.out.println(getName()+"：大锤80，小锤40，开始砸墙！");
                for (int i = 0; i < 5 ; i++) {
                    System.out.println("80！！");
                    try {
                        Thread.sleep(900);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("哐当！");
                System.out.println(getName()+":大哥搞定！");
                yu.interrupt();//gong线程中去中断yu线程的睡眠阻塞
            }
        };
        yu.start();
        gong.start();

    }
}
